package me.zhengjie.wechat.service.impl;

import me.zhengjie.wechat.domain.AdvertisingType;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.wechat.repository.AdvertisingTypeRepository;
import me.zhengjie.wechat.service.AdvertisingTypeService;
import me.zhengjie.wechat.service.dto.AdvertisingTypeDTO;
import me.zhengjie.wechat.service.dto.AdvertisingTypeQueryCriteria;
import me.zhengjie.wechat.service.mapper.AdvertisingTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author Jiang yunxiao
* @date 2019-11-10
*/
@Service
@CacheConfig(cacheNames = "advertisingType")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AdvertisingTypeServiceImpl implements AdvertisingTypeService {

    private final AdvertisingTypeRepository advertisingTypeRepository;

    private final AdvertisingTypeMapper advertisingTypeMapper;

    public AdvertisingTypeServiceImpl(AdvertisingTypeRepository advertisingTypeRepository, AdvertisingTypeMapper advertisingTypeMapper) {
        this.advertisingTypeRepository = advertisingTypeRepository;
        this.advertisingTypeMapper = advertisingTypeMapper;
    }

    @Override
    @Cacheable
    public Map<String,Object> queryAll(AdvertisingTypeQueryCriteria criteria, Pageable pageable){
        Page<AdvertisingType> page = advertisingTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(advertisingTypeMapper::toDto));
    }

    @Override
    @Cacheable
    public List<AdvertisingTypeDTO> queryAll(AdvertisingTypeQueryCriteria criteria){
        return advertisingTypeMapper.toDto(advertisingTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "#p0")
    public AdvertisingTypeDTO findById(Long id) {
        AdvertisingType advertisingType = advertisingTypeRepository.findById(id).orElseGet(AdvertisingType::new);
        ValidationUtil.isNull(advertisingType.getId(),"AdvertisingType","id",id);
        return advertisingTypeMapper.toDto(advertisingType);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public AdvertisingTypeDTO create(AdvertisingType resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return advertisingTypeMapper.toDto(advertisingTypeRepository.save(resources));
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(AdvertisingType resources) {
        AdvertisingType advertisingType = advertisingTypeRepository.findById(resources.getId()).orElseGet(AdvertisingType::new);
        ValidationUtil.isNull( advertisingType.getId(),"AdvertisingType","id",resources.getId());
        advertisingType.copy(resources);
        advertisingTypeRepository.save(advertisingType);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        advertisingTypeRepository.deleteById(id);
    }


    @Override
    public void download(List<AdvertisingTypeDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AdvertisingTypeDTO advertisingType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" name",  advertisingType.getName());
            map.put(" creationTime",  advertisingType.getCreationTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<AdvertisingType> getList() {
        return advertisingTypeRepository.findAll();
    }

}